<script setup lang="ts">
import type { FormRules } from "element-plus"
import type { LoginRequestData } from "./apis/type"
// import { Key, Loading, Lock, Picture, User } from "@element-plus/icons-vue"
import { useSettingsStore } from "@/pinia/stores/settings"
import { useUserStore } from "@/pinia/stores/user"
import { loginApi } from "./apis"
import { useFocus } from "./composables/useFocus"
import { getWechatLoginUrl  } from "@/api"
const router = useRouter()
const iframeUrl = ref("http://localhost:3338/#/wechatloginiframe")
const userStore = useUserStore()

import {wechatScanLoginApi} from "@/api"
const settingsStore = useSettingsStore()
const dialogVisible = ref(true)
const { isFocus, handleBlur, handleFocus } = useFocus()
const timer = null;
const checked = ref(true)
/** 登录表单元素的引用 */
const loginFormRef = useTemplateRef("loginFormRef")

/** 登录按钮 Loading */
const loading = ref(false)

// 处理微信登录回调
// 微信扫码成功后，会跳转到你在后端配置的 redirect_uri，
// 页面 URL 会变成这样：http://yourdomain.com/login-callback?code=xxx&state=yyy
// 你需要一个专门的页面或在当前页面处理这个回调
onMounted(() => {

  const codeString = window.location.href.split('?')[1]
  const codeString2 = codeString.split('&')[0]
  const code = codeString2.split('=')[1]

  console.log(codeString2);
  // console.log(code);
  if (code) {
    // 通常这里会调用后端的 callback 接口，用 code 换取 token
    wechatScanLoginApi({code}).then( (res) => {
      console.log(res)
      userStore.setUsername(res.data.mobile)
      userStore.setToken(res.data.token)
      userStore.setUserId(res.data.id)
      // 这里只是演示，实际项目需要一个专门的回调处理逻辑或页面
      console.log('接收到微信回调:', { code} );
    })
  }
})
</script>

<template>
  <div class="container child-view" style="max-width: 2536px;">
    微信登录成功
  </div>
</template>
<style src="@/pages/login/style/wechatlogin.css"  scoped></style>
<style>
</style>
